package tako.wisers.com.android.apis.parser;

import java.io.InputStream;
import java.util.Hashtable;
import java.util.Map;

import org.w3c.dom.Node;

import tako.wisers.com.android.apis.parser.xml.item.ButtonItem;
import tako.wisers.com.android.apis.util.Global;

public class StationXMLParser extends XMLParserBase {
	private final static String EXTERNAL_TAG = "external";
	public final static String NODE_TAG = "node";
	public final static String PARAM_TAG = "param";
	public final static String DRAWABLE_TAG = "drawable";
	public final static String HANDLER_TAG = "handler";
	
	public final static String NODE_ATTRIBUTE_NAME = "name";
	public final static String NODE_ATTRIBUTE_VALUE = "value";
	public final static String NODE_ATTRIBUTE_ACTIVITY = "activity";
	private final static String SRC_ATTR = "src";
	private final static String TYPE_ATTR = "type";
	private final static String SUB_TYPE_ATTR = "subtype";
	private final static String SHOP_ATTR = "shop";
	private final static String LOCATION_ATTR = "location";

	private final static String STATION_XML_FILE_NAME = "/list-conf.xml";
	public static final int hight = 0x6f010000;

	private static StationXMLParser msxp;

	private Map<String, ButtonItem> stationMap = new Hashtable<String, ButtonItem>();

	private StationXMLParser() {
		try {
			build(Global.CONF_PATH + STATION_XML_FILE_NAME);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private StationXMLParser(String path) {
		try {
			build(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private StationXMLParser(InputStream is) {
		try {
			build(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static StationXMLParser create(InputStream is) {
		msxp = new StationXMLParser(is);
		return msxp;
	}

	public static StationXMLParser create() {
		msxp = new StationXMLParser();
		return msxp;
	}

	public static StationXMLParser getInstance() {
		return msxp;
	}

	private void nodeTag(Node node) {
		String key = getNodeAttribute(node, NODE_ATTRIBUTE_NAME);
		String typeStr = getNodeAttribute(node, TYPE_ATTR);
		String subtypeStr = getNodeAttribute(node, SUB_TYPE_ATTR);
		String shopStr = getNodeAttribute(node, SHOP_ATTR);
		String locationStr = getNodeAttribute(node, LOCATION_ATTR);
		int type = String2Int(typeStr);
		int subtype = String2Int(subtypeStr);
		int shop = String2Int(shopStr);
		int location = String2Int(locationStr);
		int id = getID(type, subtype, shop, location);
		stationMap.put(key, new ButtonItem(hight + id, key,
				type, subtype, shop, location));
	}

	private int String2Int(String str) {
		if (str != null) {
			return new Integer(str);
		}
		return 0;
	}

	protected void tagNameSwitch(Node node) {
		String name = node.getNodeName();
		if (NODE_TAG.equalsIgnoreCase(name))
			nodeTag(node);
		else if (EXTERNAL_TAG.equalsIgnoreCase(name))
			extenalTag(node);
		else if (DRAWABLE_TAG.equalsIgnoreCase(name))
			drawableTag(node);
		else if (HANDLER_TAG.equalsIgnoreCase(name))
			handlerTag(node);

		parser(node.getChildNodes());
	}

	private void drawableTag(Node node) {
		String drawableName = getNodeAttribute(node, NODE_ATTRIBUTE_VALUE);
		Node parentNode = getParentNodeByName(NODE_TAG, node.getParentNode());
		String key = getNodeAttribute(parentNode, NODE_ATTRIBUTE_NAME);
		ButtonItem item = stationMap.get(key);
		item.setDrawName(drawableName);
		stationMap.put(key, item);
	}

	private void handlerTag(Node node) {
		String handlerName = getNodeAttribute(node, NODE_ATTRIBUTE_VALUE);
		Node parentNode = getParentNodeByName(NODE_TAG, node.getParentNode());
		String key = getNodeAttribute(parentNode, NODE_ATTRIBUTE_NAME);
		ButtonItem item = stationMap.get(key);
		item.setHandler(handlerName);
		stationMap.put(key, item);
	}

	private Node getParentNodeByName(String nodeName, Node parentnode) {
		if (parentnode.getNodeName().equalsIgnoreCase(nodeName)) {
			return parentnode;
		} else {
			return getParentNodeByName(nodeName, parentnode.getParentNode());
		}
	}

	private void extenalTag(Node node) {
		String src = getNodeAttribute(node, SRC_ATTR);
		StationXMLParser childXML = new StationXMLParser(src);
		Map<String, ButtonItem> tempTable = childXML.getStationMap();
		this.stationMap.putAll(tempTable);
	}

	public Map<String, ButtonItem> getStationMap() {
		return stationMap;
	}

	public ButtonItem getButtonById(String key) {
		return stationMap.get(key);
	}

	public void addPatch(String key, Node item) {
		if (stationMap.containsKey(key)) {
			stationMap.remove(key);
		}
		tagNameSwitch(item);
	}

	public void deleteItem(String key) {
		if (stationMap.containsKey(key)) {
			stationMap.remove(key);
		}
	}
	
	public int getID(int type, int subtype, int shop, int location){
		int id = type * 1000 + subtype*100 + shop*10 + location;
		return id;
	}

}
